//
//  ListdetailViewController.m
//  ThePrivateKitchen
//
//  Created by lirui on 16/1/25.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ListdetailViewController.h"


@interface ListdetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataArr;
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic,assign)BOOL isRefresh;
@end

static NSInteger i = 0;
@implementation ListdetailViewController

- (void)dealloc
{
    [_dataArr release];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    NSLog(@"%@",self.model.h_id);
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.title = self.model.title;
    self.dataArr = [NSMutableArray array];
    [self prepareData];
    [self creatTableView];
    [self loadMBProgress];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"left@3x"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction:)];
    
}
-(void)leftAction:(UIBarButtonItem *)left
{
    [self.navigationController popViewControllerAnimated:YES];
}

    

//数据请求
-(void)prepareData
{
    NSString *str =[NSString stringWithFormat:@"http://api.meishi.cc/v5/recipe_list.php?cid=%@&lon=4.9E-324&source=android&lat=4.9E-324&page=%ld&format=json&",self.model.h_id,i];
    [LRAFNetwork  postUrlString:str body:nil response:JSON bodyStyle:RequestJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if (self.isRefresh) {
            [self.dataArr removeAllObjects];
        }
        
        NSArray *arr =responseObject[@"data"];
        [self.dataArr  addObjectsFromArray:[ TopicModel modelHanderWithArray:arr]];
        NSLog(@"%@",_dataArr);
        [_tableView reloadData];
        [self.hud removeFromSuperview];
        [_tableView.mj_header endRefreshing];
        [_tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
//创建tableview
-(void)creatTableView
{
    self.tableView = [[ UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH *375, HEIGHT *667 -110) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [ UIColor whiteColor];
    [_tableView registerClass:[ ListdetailTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    //下拉刷新
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
        i = 1;
        self.isRefresh = YES;
        
        [self prepareData];
    }];
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
    //上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        i++;
        self.isRefresh = NO;
        [self prepareData];
    }];
    self.tableView.mj_footer = footer;
    

    
    [_tableView release];
    
}
#pragma mark -必须实现的方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   ListdetailTableViewCell *cell =[ tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    TopicModel *m = _dataArr[indexPath.row];
    cell.title.text = m.title;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.titlepic]];
    cell.detail.text = [NSString stringWithFormat:@"✨:%@/%@",m.gongyi,m.kouwei];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
//点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DarenViewController *listWeb =[[ DarenViewController alloc] init];
    listWeb.t_model = _dataArr[indexPath.row];
    [self.navigationController pushViewController:listWeb animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
